package com.kotlin.myapplication.base

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.Window
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.kotlin.myapplication.R
import com.kotlin.myapplication.utils.StatusBarUtil
import com.kotlin.myapplication.mananger.AppManager
import android.view.KeyEvent.KEYCODE_BACK


/**
 *@lil
 *@创建时间 2019/10/30 16:48
 *@描述
 */

abstract class BaseActivity : FragmentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE) // 取消标题
        initView()
        setStatusBar()
        addActivity()
    }

    abstract fun initView()

    protected fun setStatusBar() {
        StatusBarUtil.setColor(this, resources.getColor(R.color.yellow))
    }

    fun showToast(msg: String) {
        if (!isFinishing) {
            if (TextUtils.isEmpty(msg)) {
                return
            }
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        }

    }

    /**
     * 重写getResources()方法，让APP的字体不受系统设置字体大小影响
     */
    override fun getResources(): Resources {
        val res = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        res.updateConfiguration(config, res.displayMetrics)
        return res
    }

    private fun addActivity() {
        var appManager = AppManager.appManager
        appManager.addActivity(this)
    }


    override fun onDestroy() {
        super.onDestroy()
        AppManager.appManager.removeActivity(this)
        if (AppManager.appManager.currentActivity() === null) {
            System.exit(0)
        }
    }
}